var app = getApp();
var webhost = app.globalData.webhost;
var utilDate = require('../../utils/date.js');

Page({
  data: {
    currentTab: 0,
    index: 0,
    dateStart: '报账日期起',
    dateEnd: '止',
    status: '',
    statusList: ['全部'],
    statusCodeList: [''],
    driverAccount: '',
    sessionid: '',
    pageNumber: 1,
    pageSize: 5,
    detailList: [],
    loading: 0
  },

  bindStartChange: function(e) {
    var that = this;
    var date = e.detail.value;
    that.setData({
      dateStart: date,
      pageNumber: 1,
      salaryList1: [],
      salaryList2: [],
      detailList: [],
    })
    that.getInfo()
  },

  bindEndChange: function(e) {
    var that = this;
    var date = e.detail.value;
    that.setData({
      dateEnd: date,
      pageNumber: 1,
      salaryList1: [],
      salaryList2: [],
      detailList: [],
    })
    that.getInfo()
  },

  bindStatusChange: function(e) {
    var that = this;
    var index = e.detail.value;
    var status = that.data.statusCodeList[index];
    that.setData({
      index: parseInt(index),
      status: status,
      pageNumber:1,
      salaryList1: [],
      salaryList2: [],
      detailList: [],
    })
    that.getInfo()
  },

  getTangDetail: function(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var reimAccCode = e.currentTarget.dataset.code;
    wx.navigateTo({
      url: './tangDetail?id=' + id + '&reimAccCode=' + reimAccCode
    })
  },

  getWageDetail: function(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var reimAccCode = e.currentTarget.dataset.code;
    wx.navigateTo({
      url: './wageDetail?id=' + id + '&reimAccCode=' + reimAccCode
    })
  },

  getInfo: function() {
    var that = this;
    var myDate = new Date();
    var myDate2 = new Date(myDate.getTime() - 7 * 24 * 3600 * 1000);
    var date = utilDate.Format('yyyy-MM-dd', myDate);
    var date2 = utilDate.Format('yyyy-MM-dd', myDate2);
    var dateStart = that.data.dateStart == '报账日期起' ? date2 : that.data.dateStart;
    var dateEnd = that.data.dateEnd == '止' ? date : that.data.dateEnd;
    var status = that.data.status;
    var pageNumber = that.data.pageNumber;
    var pageSize = that.data.pageSize;
    var driverAccount = that.data.driverAccount;
    var sessionid = that.data.sessionid;
    var parameters = [{
      String: {
        driverAccount: app.globalData.driverAccount,
        sessionid: app.globalData.sessionid,
        dateStart: dateStart,
        dateEnd: dateEnd,
        status: status,
        pageNumber: pageNumber,
        pageSize: pageSize
      }
    }];
    var parameters2 = [{
      String: {
        driverAccount: app.globalData.driverAccount,
        sessionid: app.globalData.sessionid,
        dateStart: dateStart,
        dateEnd: dateEnd,
        status: status,
      }
    }];
    //获取报账单明细数据
    wx.request({
      url: webhost,
      data: {
        eap_username: "driver",
        eap_password: "Driver2018",
        boId: "tspdriver_tsp2DriverServiceBO_bo", //调用的bo
        methodName: 'queryReimList', //调用的具体方法
        returnType: "json", //返回参数类型
        parameters: JSON.stringify(parameters)

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function(res) {
        var code = res.data.code;
        var dataList = res.data.dataList;
        if (code == '000000') {
          if (dataList.length > 0){
            //获取到的数据中有值为null时，将其替换成''
            for (var i = 0; i < dataList.length; i++) {
              for (var key in dataList[i]) {
                if (dataList[i][key] == null) {
                  dataList[i][key] = ''
                }
                else if (typeof (dataList[i][key]) == 'number') {
                  dataList[i][key] = dataList[i][key] ? parseFloat(dataList[i][key].toFixed(2)) : ''
                }
              };
              dataList[i].reimAccDate = dataList[i].reimAccDate.substring(0, 10)
            }
            that.setData({
              detailList: that.data.detailList.concat(dataList),
              loading: 0,
            })
          }else{
            that.setData({
              loading: 2
            })
          }
          
        } else {
          wx.showModal({
            title: '提示',
            showCancel: false,
            confirmColor: '#118EDE',
            content: res.data.msg,
            success: function(res) {
              if (code == '003000') {
                if (res.confirm) {
                  wx.reLaunch({
                    url: '../login/login'
                  })
                }
              }
            }
          })
        }

      }
    })
    //获取报账单明细对应的工资统计数据
    setTimeout(function(){
      wx.request({
        url: webhost,
        data: {
          eap_username: "driver",
          eap_password: "Driver2018",
          boId: "tspdriver_tsp2DriverServiceBO_bo", //调用的bo
          methodName: 'queryDriverSlaryAll', //调用的具体方法
          returnType: "json", //返回参数类型
          parameters: JSON.stringify(parameters2)

        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: 'POST',
        success: function (res) {
          var code = res.data.code;
          var dataList1 = res.data.data;
          var dataList2 = res.data.data2;
          if (code == '000000') {
            if (dataList1) {
              //获取到的数据中有值为null时，将其替换成'',并将number数据转化为F2
              dataList1.n3 = dataList1.n3 ? parseFloat(dataList1.n3.toFixed(2)) : '';
              dataList1.n2 = dataList1.n2 ? dataList1.n2 : '';
              that.setData({
                salaryList1: dataList1,
              })
            }
            if (dataList2 && dataList2.length > 0) {
              //获取到的数据中有值为null时，将其替换成'',并将number数据转化为F2
              for (var i = 0; i < dataList2.length; i++) {
                for (var key in dataList2[i]) {
                  if (dataList2[i][key] == null) {
                    dataList2[i][key] = ''
                  }
                  else if (typeof (dataList2[i][key]) == 'number') {
                    dataList2[i][key] = dataList2[i][key] ? parseFloat(dataList2[i][key].toFixed(2)) : ''
                  }
                }
              }
              that.setData({
                salaryList2: dataList2,
              })
            }
          } else {
            wx.showModal({
              title: '提示',
              showCancel: false,
              confirmColor: '#118EDE',
              content: res.data.msg,
              success: function (res) {
                if (code == '003000') {
                  if (res.confirm) {
                    wx.reLaunch({
                      url: '../login/login'
                    })
                  }
                }
              }
            })
          }
        },
        fail: function (e) {
          wx.showToast({
            title: '网络连接错误',
            duration: 2000,
          })
        }
      })
    },500)

  },

  onLoad: function(options) {
    var that = this;
    //获取审批状态  getCodeList
    var codeList = [{
      String: {
        codeListKey: 'global_status'
      }
    }];
    wx.request({
      url: webhost,
      data: {
        eap_username: "driver",
        eap_password: "Driver2018",
        boId: "tspdriver_tsp2DriverServiceBO_bo", //调用的bo
        methodName: 'getCodeList', //调用的具体方法
        returnType: "json", //返回参数类型
        parameters: JSON.stringify(codeList)

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function(res) {
        var code = res.data.code;
        if (code == '000000') {
          var index = that.data.index;
          var statusList = [];
          var statusCodeList = [];
          for (var i = 0; i < res.data.codeList.length; i++) {
            statusList[i] = res.data.codeList[i].codeName;
            statusCodeList[i] = res.data.codeList[i].codeValue
          }
          that.setData({
            statusList: that.data.statusList.concat(statusList),
            statusCodeList: that.data.statusCodeList.concat(statusCodeList),
            status: statusCodeList[index]
          })
          that.getInfo();
        } else {
          wx.showModal({
            title: '提示',
            showCancel: false,
            confirmColor: '#118EDE',
            content: res.data.msg,
            success: function(res) {
              if (res.confirm) {}
            }
          })
        }
      },
      fail: function(res) {
        wx.showModal({
          title: '提示',
          showCancel: false,
          confirmColor: '#118EDE',
          content: res.data.msg,
          success: function(res) {
            if (res.confirm) {}
          }
        })
      },
    })

  },

  onReady: function() {

  },

  onShow: function() {

  },

  onPullDownRefresh: function() {

  },

  onReachBottom() {
    var that = this;
    if (that.data.loading == 0) {
      that.setData({
        loading: true,
        pageNumber: that.data.pageNumber + 1
      })
      setTimeout(() => {
        that.getInfo();
      }, 300);
    }
  },

  onShareAppMessage: function() {

  }
})